package cn.com.elite;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import sfmi.component.chnchannel.chncallcenter.client.CommonClient;
import sfmi.component.chnchannel.chncallcenter.client.FreeInsuranceClient;
import sfmi.component.chnchannel.chncallcenter.dto.common.Code;
import sfmi.component.chnchannel.chncallcenter.dto.common.Name;
import sfmi.component.chnchannel.chncallcenter.dto.common.key.CodeSearchKey;
import sfmi.component.chnchannel.chncallcenter.dto.enums.ConnectMode;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FICoverage;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FIPolicyholder;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FIProduct;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.FIQuotation;
import sfmi.component.chnchannel.chncallcenter.dto.freeinsurance.key.FIQuotationKey;
import sfmi.component.chnchannel.chncallcenter.exception.ChnChannelException;

public class findCommonCodeList extends HttpServlet {
	private static final Log log=LogFactory.getLog(findCommonCodeList.class);

	/**
	 * Constructor of the object.
	 */
	public findCommonCodeList() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String returnxml="";
		try{
			String UserID=request.getParameter("UserID");
			log.debug("UserID:"+UserID);
			String CodeNo=request.getParameter("CodeNo");
			log.debug("CodeNo:"+CodeNo);
			String IP=Util.getIp(request);
			log.debug("IP:"+IP);
			String otherCondition=request.getParameter("otherCondition");
			log.debug("otherCondition:"+otherCondition);
			CommonClient commonClient = new CommonClient(ConnectMode.RealServer);
	        CodeSearchKey codeSearchKey = new CodeSearchKey();		
	        codeSearchKey.setUserID(UserID);
	        codeSearchKey.setIpAddress(IP);
	        codeSearchKey.setCodeNo(CodeNo);
	        codeSearchKey.setOtherCondition(otherCondition);
	        List<Code> codeList=commonClient.findCommonCodeList(codeSearchKey);
	        if(codeList!=null&&codeList.size()>0){
	        	Document result=Util.getDoc();
	        	Element root=result.addElement("Result");
            	for(int i=0;i<codeList.size();i++){
            		Code code=codeList.get(i);
            		log.debug(code.getCode());
            		log.debug(code.getEnglishName());
            		log.debug(code.getName());
            		Element R=root.addElement("R");
            		R.addElement("Code").addText(code.getCode());
            		R.addElement("EnglishName").addText(code.getEnglishName());
            		R.addElement("Name").addText(code.getName());
            	}
            	returnxml=result.asXML();
            }	
		}catch(Exception e){
			log.error(Util.getERRO(e));
		}
		request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=utf-8;");
	    PrintWriter out = response.getWriter();
	    log.debug("xml:"+returnxml);
	    out.print(returnxml);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
